package com.wlyy.his.service.pc.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wlyy.his.bean.user.pc.dto.MrdFindDto;
import com.wlyy.his.common.exception.Asserts;
import com.wlyy.his.entity.pc.MedicalRecodeNumber;
import com.wlyy.his.entity.pc.Patient;
import com.wlyy.his.entity.pc.PatientCertificate;
import com.wlyy.his.mapper.pc.MedicalRecodeNumberMapper;
import com.wlyy.his.mapper.pc.PatientCertificateMapper;
import com.wlyy.his.mapper.pc.PatientMapper;
import com.wlyy.his.service.pc.IMedicalRecodeNumberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 病案号 服务实现类
 * </p>
 *
 * @author zh
 * @since 2021-09-02
 */
@Service
public class MedicalRecodeNumberServiceImpl extends ServiceImpl<MedicalRecodeNumberMapper, MedicalRecodeNumber> implements IMedicalRecodeNumberService {

    @Resource
    private MedicalRecodeNumberMapper medicalRecodeNumberMapper;
    @Resource
    private PatientMapper patientMapper;
    @Resource
    private PatientCertificateMapper patientCertificateMapper;


    @Override
    public String bindMrd(MrdFindDto mrdFindDto) {
        List<PatientCertificate> patientCertificates = patientCertificateMapper.selectList(Wrappers.lambdaQuery(PatientCertificate.class)
                .eq(PatientCertificate::getPatientId, mrdFindDto.getPatientId()));
        if (BeanUtil.isNotEmpty(patientCertificates)){
            //根据证件号嗲用医院接口看his中是否存在病案号
            boolean exist = true;
            if (exist){
                Patient patient = patientMapper.selectOne(Wrappers.lambdaQuery(Patient.class)
                        .eq(Patient::getPatientId, mrdFindDto.getPatientId()));
                if (null!=patient){
                    //拿着平台预留的手机号，姓名，证件跟院内的预留信息匹配匹配上了就绑定，匹配不上就绑定失败

                    boolean match = true;
                    if (match){

                    }else {
                        Asserts.fail("绑定失败，就诊人姓名和院内预留信息不一致，无法绑定");
                    }

                }
            }else{
                Asserts.fail("该就诊人没有在医院建过档，暂无法使用该功能");
            }

        }
        return "";
    }

    @Override
    public String getMRD(MrdFindDto mrdFindDto) {
        MedicalRecodeNumber medicalRecodeNumber = medicalRecodeNumberMapper.selectOne(Wrappers.lambdaQuery(MedicalRecodeNumber.class)
                .eq(MedicalRecodeNumber::getMedicalRecordNumber, mrdFindDto.getPatientId())
                .eq(MedicalRecodeNumber::getHospitalNumber, mrdFindDto.getHospitalNumber()));

        if (null!=medicalRecodeNumber){
            return medicalRecodeNumber.getMedicalRecordNumber();
        }
        return "";
    }
}
